package com.lyp.springboot.controller;

import com.lyp.springboot.service.UserService;
import com.lyp.springboot.support.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(path = {"/toLogin"})
    public String toLogin (){
        return "index";
    }

    @RequestMapping("/crm_user")
    public ActionResult userLogin(HttpServletRequest request, Model model){
        String name = request.getParameter("userName");
        String password = request.getParameter("password");
        String msg = "登陆失败！";
        if(userService.checkLoginUser(name,password)>0){
            msg = "登陆成功！";
        }
        return new ActionResult(userService.checkLoginUser("admin","admin")+"",msg);
    }
    @RequestMapping(path = {"/saveUser"})
    public ActionResult saveUser (HttpServletRequest request, Model model){
        String name = request.getParameter("name");
        String realName = request.getParameter("realName");
        String passwordOne = request.getParameter("passwordOne");
        String passwordTwo = request.getParameter("passwordTwo");
        System.out.println("save user");
        if(!passwordOne.equals(passwordTwo)){
            return new ActionResult("0","两次密码不一致！");
        }
        userService.saveUser(name,realName,passwordOne,"1","lyp");
        return ActionResult.SUCCESS;
    }
}
